
//BattleList 管理战斗列表数据

//为 State_BattleChoose模块提供数据
import { tt } from "../../ttapi_interface/ttapi.js";
import * as tt2 from "../../ttapi_layer2/ttlayer2.js";

export enum BattleMode {
    Battle,//单场战斗，点击他直接进入战斗
    Level,//一场战役，包含N场战斗
    Level_Graph,//一个图形化战役，一个有地图显示的选关界面
}
export class BattleItem {
    name: string;
    mode: BattleMode;
    need: string[] = null;//需要完成的前置管卡
    config: string = null;
    children: BattleItem[] = null;
    parent: BattleItem = null;
}
//BattleList 管理战斗列表
export class BattleList {
    private rootlist: BattleItem[] = [];
    GetList(parent: BattleItem = null): BattleItem[] {
        if (parent == null)
            return this.rootlist;

        return parent.children;

        return null;
    }
    async LoadAsync(jsonurl: string): Promise<void> {
        let txt = await tt.loader.LoadStringAsync(tt2.QFrame_ResMgr.GetDefResPath() + jsonurl);
        let list = JSON.parse(txt) as any[];
        BattleList.ParseList(list, this.rootlist);
        
    }
    private static ParseList(json: any[], list: BattleItem[]) {
        for (var i = 0; i < json.length; i++) {
            let item = new BattleItem();
            item.name = json[i].name.toLowerCase();
            let need = json[i].need as string[];
            if (need != undefined) {
                item.need = [];
                for (var j = 0; j < need.length; j++) {
                    item.need.push(need[j].toLowerCase());
                }
            }
            let m = (json[i].mode as string).toLowerCase();
            if (m == "level")
                item.mode = BattleMode.Level;
            else if (m == "battle")
                item.mode = BattleMode.Battle;
            else if (m == "level_graph")
                item.mode = BattleMode.Level_Graph;
            else
                throw new Error("unknow mode:" + list[i].mode);

            item.config = json[i].config;
            let children = json[i].subitems as any[];
            if (children != undefined) {
                item.children = [];
                BattleList.ParseList(children, item.children)
                for (var j = 0; j < item.children.length; j++) {
                    item.children[j].parent = item;
                }
            }

            list.push(item);
        }
    }
}